//
//  ButtonViewController.swift
//  MacStudy
//
//  Created by 30san on 2020/11/18.
//  Copyright © 2020 Dawninest. All rights reserved.
//

import Cocoa

class ButtonViewController: NSViewController {

    
    private lazy var mainPanel: NSPanel = {
        let frame = NSRect(x: 0, y: 0, width: 400, height: 300)
        let panel = NSPanel(contentRect: frame, styleMask: NSWindow.StyleMask.borderless, backing: NSWindow.BackingStoreType.buffered, defer: true)
        
        panel.title = "第一个panel面板"
        let redView = NSView(frame: NSRect(x: 0, y: 0, width: 200, height: 80))
        redView.wantsLayer = true
        redView.layer?.backgroundColor = NSColor.red.cgColor
        panel.contentView?.addSubview(redView)
        
        let closeButton = NSButton(frame: NSRect(x: 300, y: 0, width: 80, height: 40))
        closeButton.title = "关闭"
        closeButton.action = #selector(closePanel)
        closeButton.target = self
        panel.contentView?.addSubview(closeButton)
        
        return panel
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    private lazy var alert: NSAlert = {
        let alert = NSAlert()
        alert.messageText = "Alert"
        alert.informativeText = "informativeText"
        alert.icon = NSImage(named: "icon")
        /**
         case warning = 0

         case informational = 1
         
         case critical = 2
         */
        alert.alertStyle = .warning
        alert.addButton(withTitle: "确定")
        alert.addButton(withTitle: "取消")
        return alert
    }()
    
    @IBAction func showPanel(_ sender: Any) {
        // 打开
        view.window?.beginSheet(mainPanel, completionHandler: { (response) in
            print("beginSheet - \(response)")
        })
    }
    
    @objc func closePanel() {
        // 关闭
        view.window?.endSheet(mainPanel)
        print("closePanel")
    }
    
    
    /// 显示弹框
    @IBAction func showAlert(_ sender: Any) {
//        alert.beginSheetModal(for: view.window!) { (response) in
//            /**
//             response = NSModalResponse(rawValue: 1001)
//             response = NSModalResponse(rawValue: 1000)
//             根据加入按钮顺序来判断点击的按钮
//             */
//            print("response = \(response)")
//        }
        
        alert.runModal()
    }
}
